package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.R;
import com.example.demo.entity.Address;
import com.example.demo.service.AddressService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import static com.example.demo.common.R.success;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 地址表(Address)表控制层
 *
 * @author makejava
 * @since 2025-08-26 16:13:31
 */
@Tag(name = "地址管理", description = "地址相关的CRUD操作")
@RestController
@RequestMapping("address")
public class AddressController {
    /**
     * 服务对象
     */
    @Resource
    private AddressService addressService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param address 查询实体
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有地址数据", description = "根据条件分页查询地址列表")
    @Parameters({
        @Parameter(name = "current", in = ParameterIn.QUERY, description = "当前页码", required = false, schema = @Schema(type = "integer", defaultValue = "1")),
        @Parameter(name = "size", in = ParameterIn.QUERY, description = "每页显示数量", required = false, schema = @Schema(type = "integer", defaultValue = "10")),
        @Parameter(name = "userId", in = ParameterIn.QUERY, description = "用户ID", required = false, schema = @Schema(type = "integer")),
        @Parameter(name = "linkUser", in = ParameterIn.QUERY, description = "联系人", required = false, schema = @Schema(type = "string"))
    })
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping
    @SaCheckPermission("user.get")
    public R selectAll(Page<Address> page, Address address) {
        return success(this.addressService.page(page, new QueryWrapper<>(address)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条地址数据", description = "根据地址ID获取地址详情")
    @Parameter(name = "id", in = ParameterIn.PATH, description = "地址ID", required = true, schema = @Schema(type = "integer"))
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.addressService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param address 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增地址数据", description = "添加新的地址信息")
    @ApiResponse(responseCode = "200", description = "新增成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PostMapping
    public R insert(@RequestBody Address address) {
        return success(this.addressService.save(address));
    }

    /**
     * 修改数据
     *
     * @param address 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改地址数据", description = "更新地址信息")
    @ApiResponse(responseCode = "200", description = "修改成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PutMapping
    public R update(@RequestBody Address address) {
        return success(this.addressService.updateById(address));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @Operation(summary = "删除地址数据", description = "批量删除地址")
    @Parameter(name = "idList", in = ParameterIn.QUERY, description = "地址ID列表", required = true, schema = @Schema(type = "array", implementation = Long.class))
    @ApiResponse(responseCode = "200", description = "删除成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.addressService.removeByIds(idList));
    }
}

